<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户信息</title>
<%@ include file="../common/common.jsp"%>
<script type="text/javascript" 
src="<%=request.getContextPath()%>/js/selectOption.js"></script>
</head>
<script type="text/javascript">

//新增用户
function newCp_info(){
	$('#dlg').dialog('open').dialog('setTitle', '新增用户');
	jQuery('#companyCode').combobox({
	    url:'<%=basePath%>jsp/GetJson.jsp?type=companyinfoList',
	    valueField:'companyCode',
	    textField:'companyName',
	    required:true
	});
	
	$('#fm').form('clear');
}
//编辑用户信息
function editCp_info() {
	var row = $('#dg').datagrid('getSelected');
	if (row) {
		$('#dlg').dialog('open').dialog('setTitle', '编辑用户');
		jQuery('#companyCode').combobox({
		    url:'<%=basePath%>jsp/GetJson.jsp?type=companyinfoList',
		    valueField:'companyCode',
		    textField:'companyName'
		});
		$('#companyCode').combobox('setText',row.companyCode);
		$('#fm').form('load', row);
	}
}

//保存用户信息
function saveCp_info() {
	$('#fm').form('submit', {
		url : '<%=basePath%>saveUserinfoAction!saveData.do',
		onSubmit : function() {
			return $(this).form('validate');
		},
		success : function(result) {
			var result = eval('(' + result + ')');
			//alert(result);
			if (result.message=='success') {
				$('#dlg').dialog('close'); // close the dialog
				$('#dg').datagrid('reload'); // reload the user data
		} else {
		$.messager.show( {
			title : '错误提示',
			msg : result.message
		});
	}
}
	});
}



function removeCp_info() {
	var row = $('#dg').datagrid('getSelected');
	if (row) {
		$.messager.confirm('Confirm',
				'请确认是否删除该用户信息?', function(r) {
					if (r) {
						$.post('<%=basePath%>saveUserinfoAction!destoryData.do', {
							id : row.id
						}, function(result) {
							if (result.message=='success') {
								$('#dg').datagrid('reload'); // reload the user data
							} else {
								$.messager.show( { // show error message
										title : '错误提示',
										msg : result.message
										});
							}
						}, 'json');
					}
				});
	}
}

function formatAction(value,row,index){
	if (row.editing){
		var s = '<a href="#" onclick="saverow('+index+')">保存</a> ';
		var c = '<a href="#" onclick="cancelrow('+index+')">取消</a>';
		return s+c;
	} else {
		var e = '<a href="#" onclick="f_auth('+row.id+')">查看并分配角色</a> ';
		return e;
	}
}

 function f_auth(id){
	 
	 $('#dlg—role').dialog('open').dialog('setTitle', '角色信息');
	 $('#user_id').attr('value',id);
	 
	 //清空
	 $("#rightselect").empty(); 
	 $("#leftselect").empty();
	 
	 var selectedResult = []; 

	
	
	//获得已分配的角色
	var s_url = '<%=basePath%>loadUserRoleAction.do';
	var params = {user_id:id};

		jQuery.post(s_url,params,function(result){
			//alert(result.rows);
			if(result!=null||result!=''){
				result = result.rows;
				$.each(result,function(index,d){
						   var description = d['description'];
						   var role_id = d['role_id'];
						  //alert(d['description']);
						  selectedResult.push(role_id);
					  	   $('#rightselect').append("<option value='"+role_id+"'>"+description+"</option>");
					   })
				}		   
		
		},'json'); 
	
		//获得角色列表
		var url = '<%=basePath%>loadRoleAction!loadData.do';
		var params = [];
		jQuery.post(url,params,function(result){
			//alert(result);
			if(result!=null||result!=''){
				result = result.rows;
			 $.each(result,function(index,d){
					   var description = d['description'];
					   var role_id = d['role_id'];
					  //alert(d['description']);
					   for(var i=0 ;i<selectedResult.length;i++){
						   
						   if(selectedResult[i]!=role_id){
							   $('#leftselect').append("<option value='"+role_id+"'>"+description+"</option>");
						   }
					   }
				  	   
				   })
			}		   
		
	},'json'); 
		
		
		
		
		
		
} 


//保存角色信息
function save_f_auth() {
	
	var user_id = $('#user_id').attr('value');
	var role_id = $('#rightselect').attr('value');
	alert(role_id);
	var params = {
			user_id:user_id,
			role_id:role_id
			};
	var url = '<%=basePath%>saveUserinfoAction!saveUserRoleInfo.do';
	
	/*  jQuery.post(url,params,function(result){
			if (result.message=='success') {
				$('#dlg—role').dialog('close'); // close the dialog
				$('#dg').datagrid('reload'); // reload the user data
			} else {
			$.messager.show( {
				title : '错误提示',
				msg : result.message
			});
		}
		
	},'json');  */
	
	
}


 
$(function(){
	
	//布局
	$('#pp').portal({
		border:false,
		fit:true
	});
	
	//组织树
	$('#tt').tree({
		url:"<%=basePath%>getJsonAction!getCompanyListTree.do",
		onClick:function(node){
			//$('#tt').tree('beginEdit', node.target);
			//alert(node.id);
			var companyCode = node.id;
			
			var url = "<%=basePath%>loadUserInfoAction!loadData.do?"+
			"userinfo.companyCode="+companyCode;
			
			if(companyCode=='000'){
				url = "<%=basePath%>loadUserInfoAction!loadData.do"
			}
		
			var params = {} ;
			jQuery.post(url,params,function(data){
				$('#dg').datagrid("loadData",data);
			
			},'json');
					
			
		}
	});
});



//查询
function search_data(){	
	var name =$("#username").attr('value');
	var url = "<%=basePath%>loadUserInfoAction!loadData.do?"+
			"userinfo.username="+name;
	var params = {} ;
	jQuery.post(url,params,function(data){
		//alert(data);
		$('#dg').datagrid("loadData",data);
	
	},'json');
}



</script>



<body class="easyui-layout">
<div region="center" border="false">
   <div id="pp" style="position:relative">
	<div style="width:20%;">
	
			<div title="公司信息" collapsible="true" closable="true" style="height:390px;padding:5px;">
			     <ul id="tt" animate="true"></ul>
			</div>
		  
	</div>
	<div style="width:80%;">
	
	
		<table  width="875px"  height="20px" style="text-align:center;background:#f3eeaf;border:1px solid #b3dfda">
					<tr>
						
						<td width="60">用户名字: </td>
						<td><input  style="width:150px" id="username" name="username"></td>
						<td><a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="search_data()">查询</a></td>
					</tr>		
			</<table>
	
			<table id="dg" title="用户信息" class="easyui-datagrid"
				style="width: 872px; height: 350px"
				url="<%=basePath%>loadUserInfoAction!loadData.do"
				toolbar="#toolbar" pagination="true" rownumbers="true"
				fitColumns="true" singleSelect="true">
				<thead>
					<tr>
						<th field="useraccount" width="20">用户帐号</th>
						<th field="username" width="30">用户名字</th>
						<th field="companyName" width="30">所属机构</th>
						<th field="phone" width="30">联系电话</th>
						<th field="email" width="30">邮箱</th>
						<th field="action" width="30" formatter="formatAction">分配角色</th>
						<th field="companyCode" width="30" hidden='true'></th>
					</tr>
				</thead>
			</table>
		
		
			<div id="toolbar">
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
					onclick="newCp_info()">新建</a> <a href="#" class="easyui-linkbutton"
					iconCls="icon-edit" plain="true" onclick="editCp_info()">编辑</a> <a
					href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
					onclick="removeCp_info()">删除</a>
			</div>
		
			<div id="dlg" class="easyui-dialog"
				style="width: 600px; height: 300px;" closed="true"
				buttons="#dlg-buttons">
				
				<form id="fm" method="post">
					<table style="border:1px solid #b3dfda;width:550px">
		
						<tr colspan="2" style="display: none">
		
							<td>
								<div class="fitem">
									<label> ID: </label> <input name="id" class="easyui-validatebox">
								</div></td>
		
						</tr>
		
						<tr height="50px">
							<td>
								<div class="fitem">
									<label> 用户帐号: </label> <input name="useraccount"
										class="easyui-validatebox" required="true">
								</div>
							</td>
								 
							<td > 
							
								<div class="fitem">
									<label> 用户名字: </label> <input name="username"
										class="easyui-validatebox" required="true">
								</div>
								
								
						    </td>
						</tr>
						
						<tr  height="50px">
							<td>
								<div class="fitem" >
									<label> 所属机构: </label>
									<!-- <input name="companyCode" class="easyui-validatebox" required="true">-->
									<select id="companyCode" class="easyui-combotree"
										name="companyCode" style="width: 140px;"></select>
								</div>
						    </td>
								
								<td > 
							
								<div class="fitem">
									<label> 联系电话: </label> <input name="phone"
										class="easyui-validatebox" required="true">
								</div>
								
						    </td>
						</tr>
						
						<tr  height="50px">
								
								<td colspan="2"> 
							
								<div class="fitem">
									<label> 邮箱: </label> <input name="email"
										class="easyui-validatebox" >
								</div>
								
						    </td>
						</tr>
						
		
					</table>
		
				</form>
			</div>
			
			
			
			<div id="dlg-buttons">
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
					onclick="saveCp_info()">保存</a> <a href="#"
					class="easyui-linkbutton" iconCls="icon-cancel"
					onclick="javascript:$('#dlg').dialog('close')">取消</a>
			</div>
			
			
			<div id="dlg—role" class="easyui-dialog"
				style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
				buttons="#dlg-role-buttons">
				<form id="rolefm">
				<input type="hidden" name="user_id" id="user_id">
				<input type="hidden" name="role_id" id="role_id">
			<table align="center"  style="text-align:center;background:#f3eeaf;border:1px solid #b3dfda" >	
			<tr>
				<td>
					可选择角色
				</td>
				<td></td>
				<td>
					已选择角色
				</td>
			</tr>
			<tr>
				<td align="center">
					<select multiple="multiple" id="leftselect" name="leftselect"
						style="width: 120px; height: 160px;">
						
					</select>

				</td>
				<td>
					<input type="button" class="add" id="add" >
					<br>
					<br>
					<br>
					<input type="button" class="remove" id="remove" >
					<br>
					
				</td>
				<td align="center">
					<select multiple="multiple" id="rightselect" name="rightselect"
						style="width: 120px; height: 160px;">
					</select>
				</td>
			</tr>
			<!--  
			<tr>
				<td align="center">
					<input id="left_up" class="button" type="button" value="向上" />
					&nbsp;&nbsp;&nbsp;
					<input id="left_down" class="button" type="button" value="向下" />
				</td>
				<td></td>
				<td align="center">
					<input id="right_up" class="button" type="button" value="向上" />
					&nbsp;&nbsp;&nbsp;
					<input id="right_down" class="button" type="button" value="向下" />
				</td>
			</tr>
			-->
		</table>					
										
										
				</form>
			</div>
			
			<div id="dlg-role-buttons">
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
					onclick="save_f_auth()">保存</a><a href="#"
					class="easyui-linkbutton" iconCls="icon-cancel"
					onclick="javascript:$('#dlg—role').dialog('close')">取消</a>
			</div>
			
	
	
	</div>




	</div>
</div>

</body>


</html>